package com.ccrfid.rmc6.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String toDateStr(Date date) {
        if (date == null) {
            return null;
        } else {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return dateFormat.format(date);
        }
    }

    public static String toTimeStr(Date date) {
        if (date == null) {
            return null;
        } else {
            DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            return dateFormat.format(date);
        }
    }

    public static String toDateTimeStr(Date date) {
        if (date == null) {
            return null;
        } else {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.format(date);
        }
    }

    public static String toDateTimeStr2(Date date) {
        if (date == null) {
            return null;
        } else {
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            return dateFormat.format(date);
        }
    }

    public static String toDateTimeStr(Date date, String formatPattern) {
        if (date == null) {
            return null;
        } else {
            DateFormat dateFormat = new SimpleDateFormat(formatPattern);
            return dateFormat.format(date);
        }
    }

    //dateTimeStr "yyyy-MM-dd HH:mm:ss"
    public static Date parseDateTimeStr(String dateTimeStr) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(dateTimeStr);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static Date mergeDateAndTime(java.sql.Date date, java.sql.Time time) {
        String dateTimeStr = date.toString() + " " + time.toString();
        return parseDateTimeStr(dateTimeStr);
    }

    public static Date mergeDateAndTime(Date date, Date time) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String dateTimeStr = dateFormat.format(date) + " " + timeFormat.format(time);
        return parseDateTimeStr(dateTimeStr);
    }

    //获取一天后的时间
    public static Date getOneDayLater(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
        return calendar.getTime();
    }

    //获取一天前的时间
    public static Date getOneDayBefore(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
        return calendar.getTime();
    }

    //获取时间值
    public static Date getTimeValue(Date date) {
        DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String timeValueStr = timeFormat.format(date);
        try {
            return timeFormat.parse(timeValueStr);
        } catch (ParseException ex) {
            return null;
        }
    }

    public static Date parseDateStr(String dateTimeStr) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(dateTimeStr);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
